package com.unionftech.common.permissions.tip.impl.view;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

import com.unionftech.common.R;
import com.unionftech.common.common.language.DynamicResourceManager;

import org.w3c.dom.Text;

public class PermissionGotoSettingDialog extends DialogFragment {
    private final Context mContext;
    private String title;
    private String message;
    private TextView mTvTitle;
    private TextView mTvMessage;
    private OnClickListener listener;

    public PermissionGotoSettingDialog(Context context) {
        this.mContext = context;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public void setListener(OnClickListener listener) {
        this.listener = listener;
    }

    @Override
    public void onStart() {
        Dialog dialog = getDialog();
        if (dialog != null) {
            Window window = dialog.getWindow();
            if (window != null) {
                WindowManager.LayoutParams params = window.getAttributes();
                params.width = WindowManager.LayoutParams.MATCH_PARENT;
                params.height = WindowManager.LayoutParams.WRAP_CONTENT;
                window.setAttributes(params);
            }
        }
        if (mTvTitle != null) {
            mTvTitle.setText(title);
        }
        if (mTvMessage != null) {
            mTvMessage.setText(message);
        }
        super.onStart();
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        Dialog dialog = getDialog();
        if (dialog != null) {
            Window window = dialog.getWindow();
            if (window != null) {
                window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
                window.setGravity(Gravity.CENTER);
                window.getDecorView().setPadding(0, 0, 0, 0);
            }
        }
        return LayoutInflater.from(mContext).inflate(R.layout.permission_goto_settings_dialog, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mTvTitle = view.findViewById(R.id.dialog_title);
        mTvMessage = view.findViewById(R.id.tv_message_dialog);
        TextView mBtnCancel = ((TextView) view.findViewById(R.id.dialog_cancel));
        mBtnCancel.setText(DynamicResourceManager.getString(R.string.cancel));
        mBtnCancel.setOnClickListener(v -> {
            dismiss();
        });
        TextView mBtnSure = ((TextView) view.findViewById(R.id.dialog_sure));
        mBtnSure.setText(DynamicResourceManager.getString(R.string.confirm));
        mBtnSure.setOnClickListener(v -> {
            if (listener != null) {
                listener.confirm();
                dismiss();
            }
        });
    }

    public interface OnClickListener {
        void confirm();
    }
}
